﻿/*
 *    Copyright [2010] [Daniel Sawan]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package com.kikoulol.jarmory.extractor.xml;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Node;

import com.kikoulol.jarmory.extractor.CharacterStats;
import com.kikoulol.jarmory.extractor.enums.Regions;

/**
 * 
 * Properties whose getter do not fire super.setupXml() method : charUrl
 * 
 */
public class CharacterSheet extends AbstractArmoryXmlFile
{
  private String charUrl;

  private CharacterStats stats;
  private List<TeamInfo> arenaTeams = new ArrayList<TeamInfo>();
  private List<ItemTooltip> items = new ArrayList<ItemTooltip>();
  
  private String battleGroup;
  private String classId;
  private String classType;
  private String classUrl;
  private String faction;
  private String factionId;
  private String gamesPlayed;
  private String gamesWon;
  private String gender;
  private String genderId;
  private String guild;
  private String guildId;
  private String guildName;
  private String guildUrl;
  private String lastModified;
  private String level;
  private String name;
  private String points;
  private String prefix;
  private String race;
  private String raceId;
  private String realm;
  private String seasonGamesPlayed;
  private String seasonGamesWon;
  private String suffix;
  private String teamRank;
  private String titleId;

  /**
   * 
   * @param region
   * @param characterQueryString
   *          part of the url that comes behind the ?
   */
  public CharacterSheet(Regions region, String characterQueryString)
  {
    super(region, "character-sheet.xml?" + characterQueryString);
    this.charUrl = characterQueryString;
  }

  /**
   * 
   */
  protected void initProperties()
  {
    Node characterSheetNode = this.getXml().getElementsByTagName("characterInfo").item(0).getChildNodes().item(1);

    this.battleGroup =  this.getNamedItemValue(characterSheetNode, "battleGroup");
    this.classId =      this.getNamedItemValue(characterSheetNode, "classId");
    this.classType =    this.getNamedItemValue(characterSheetNode, "class");
    this.classUrl =     this.getNamedItemValue(characterSheetNode, "classUrl");
    this.faction =      this.getNamedItemValue(characterSheetNode, "faction");
    this.factionId =    this.getNamedItemValue(characterSheetNode, "factionId");
    this.gamesPlayed =  this.getNamedItemValue(characterSheetNode, "gamesPlayed");
    this.gamesWon =     this.getNamedItemValue(characterSheetNode, "gamesWon");
    this.gender =       this.getNamedItemValue(characterSheetNode, "gender");
    this.genderId =     this.getNamedItemValue(characterSheetNode, "genderId");
    this.guildName =    this.getNamedItemValue(characterSheetNode, "guildName");

    if (this.getGuildName() != null)
    {
      this.guild =      this.getNamedItemValue(characterSheetNode, "guild");
      this.guildId =    this.getNamedItemValue(characterSheetNode, "guildId");
      this.guildUrl =   this.getNamedItemValue(characterSheetNode, "guildUrl");
    }

    this.lastModified = this.getNamedItemValue(characterSheetNode, "lastModified");
    this.level =        this.getNamedItemValue(characterSheetNode, "level");
    this.name =        this.getNamedItemValue(characterSheetNode, "name");
    this.points =       this.getNamedItemValue(characterSheetNode, "points");
    this.prefix =       this.getNamedItemValue(characterSheetNode, "prefix");
    this.race =         this.getNamedItemValue(characterSheetNode, "race");
    this.raceId =       this.getNamedItemValue(characterSheetNode, "raceId");
    this.realm =       this.getNamedItemValue(characterSheetNode, "realm");
    this.seasonGamesPlayed = this.getNamedItemValue(characterSheetNode, "seasonGamesPlayed");
    this.seasonGamesWon = this.getNamedItemValue(characterSheetNode, "seasonGamesWon");
    this.suffix =       this.getNamedItemValue(characterSheetNode, "suffix");
    this.teamRank =     this.getNamedItemValue(characterSheetNode, "teamRank");
    this.titleId =      this.getNamedItemValue(characterSheetNode, "titleId");

    // stats = new CharacterStats(characterXml);
  }

  /**
   * 
   * @return
   */
  public List<TeamInfo> getArenaTeams()
  {
    super.setupXml();
    
    for (int i = 0; i < this.getXml().getElementsByTagName("arenaTeam").getLength(); i++)
    {
      Node arenaTeamNode = this.getXml().getElementsByTagName("arenaTeam").item(i);
      TeamInfo arenaTeam = new TeamInfo(this.getRegion(), this.getNamedItemValue(arenaTeamNode, "url"));

      this.arenaTeams.add(arenaTeam);
    }
    return arenaTeams;
  }

  public String getBattleGroup()
  {
    super.setupXml();
    return battleGroup;
  }

  /**
   * Do not fire super.setupXml() method
   * 
   * @return
   */
  public String getCharUrl()
  {
    return charUrl;
  }

  public String getClassId()
  {
    super.setupXml();
    return classId;
  }

  public String getClassType()
  {
    super.setupXml();
    return classType;
  }

  public String getClassUrl()
  {
    super.setupXml();
    return classUrl;
  }

  public String getFaction()
  {
    super.setupXml();
    return faction;
  }

  public String getFactionId()
  {
    super.setupXml();
    return factionId;
  }

  public String getGamesPlayed()
  {
    super.setupXml();
    return gamesPlayed;
  }

  public String getGamesWon()
  {
    super.setupXml();
    return gamesWon;
  }

  public String getGender()
  {
    super.setupXml();
    return gender;
  }

  public String getGenderId()
  {
    super.setupXml();
    return genderId;
  }

  public String getGuild()
  {
    super.setupXml();
    return guild;
  }

  public String getGuildId()
  {
    super.setupXml();
    return guildId;
  }

  public String getGuildName()
  {
    super.setupXml();
    return guildName;
  }

  public String getGuildUrl()
  {
    super.setupXml();
    return guildUrl;
  }

  /**
   * 
   * @return
   */
  public List<ItemTooltip> getItems()
  {
    super.setupXml();
    
    for (int i = 0; i < this.getXml().getElementsByTagName("item").getLength(); i++)
    {
      Node itemNode = this.getXml().getElementsByTagName("item").item(i);
      ItemTooltip item = new ItemTooltip(this.getRegion(), this.getNamedItemValue(itemNode, "id"));

      this.items.add(item);
    }
    return items;
  }

  public String getLastModified()
  {
    super.setupXml();
    return lastModified;
  }

  public String getLevel()
  {
    super.setupXml();
    return level;
  }
  
  public String getName()
  {
    super.setupXml();
    return name;
  }

  public String getPoints()
  {
    super.setupXml();
    return points;
  }

  public String getPrefix()
  {
    super.setupXml();
    return prefix;
  }

  public String getRace()
  {
    super.setupXml();
    return race;
  }

  public String getRaceId()
  {
    super.setupXml();
    return raceId;
  }

  public String getRealm()
  {
    super.setupXml();
    return realm;
  }

  public String getSeasonGamesPlayed()
  {
    super.setupXml();
    return seasonGamesPlayed;
  }

  public String getSeasonGamesWon()
  {
    super.setupXml();
    return seasonGamesWon;
  }

  public CharacterStats getStats()
  {
    super.setupXml();
    return stats;
  }

  public String getSuffix()
  {
    super.setupXml();
    return suffix;
  }

  public String getTeamRank()
  {
    super.setupXml();
    return teamRank;
  }

  public String getTitleId()
  {
    super.setupXml();
    return titleId;
  }
}